#!/usr/bin/python
# -*- coding: utf-8 -*-

from db.user_db import UserDB
from utils.common import md5 

class UserManager:
    
    def __init__(self):
        '''Initialize userdb.'''
        self.userdb = UserDB() 

    def register(self, user):
        exists = self.check(user.email)
        if exists is True:
            return None 
        else:
            user.passwd = md5(user.passwd) 
            return self.userdb.register(user)

    def auth(self, user):
        user.passwd = md5(user.passwd) 
        return self.userdb.auth(user)

    def check(self, email):
        return self.userdb.check(email)

    #for test
    def delete(self, user):
        return self.userdb.delete(user.email)

    def reset(self, user):
        user.passwd = md5_passwd(user.passwd)
        return self.userdb.reset(user)

if __name__ == '__main__':
    manager = UserManager()
    from model.user import User
    user = User('gzssj', 'ssjhust')
    manager.register(user)
